package bjtu.cit.bookshop.service.impl;

import bjtu.cit.bookshop.daos.NoteMapper;
import bjtu.cit.bookshop.entities.Book;
import bjtu.cit.bookshop.entities.Note;
import bjtu.cit.bookshop.entities.NoteWithBLOBs;
import bjtu.cit.bookshop.service.INoteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by jia19 on 2015/9/21.
 */
@Service("noteService")
public class NoteServiceImpl implements INoteService {

    @Resource
    private NoteMapper noteMapper = null;


    @Override
    public NoteWithBLOBs getNoteById(int id) {
        return noteMapper.selectByPrimaryKey(id);
    }

    @Override
    public int getNoteLength() {
        return this.noteMapper.selectNoteList().size();
    }

    @Override
    public List<NoteWithBLOBs> getNoteList(int num, int offset) {
        List<NoteWithBLOBs> notes = this.noteMapper.selectNoteList();
        int endIndex = offset + num;
        if (offset >= notes.size()) {
            return null;
        }
        if (endIndex >= notes.size()) {
            endIndex = notes.size();
        }

        return notes.subList(offset, endIndex);
    }

    @Override
    public int updateNoteInfoById(NoteWithBLOBs note) {
        return this.noteMapper.updateByPrimaryKeySelective(note);
    }

    @Override
    public int deleteNoteById(int id) {
        return this.noteMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int createANote(NoteWithBLOBs note) {
        return this.noteMapper.insertSelective(note);
    }
}
